//
//  CityBDetailViewController.m
//  TravelDiary
//
//  Created by lanou3g on 15/11/14.
//  Copyright © 2015年 YDB MAC. All rights reserved.
//

#import "CityBDetailViewController.h"
#import "FanProgress3D.h"
#import <AVOSCloud.h>


#define kQJDetailUrl @"http://www.quanjingke.com/index.php?m=space&c=space_album1&a=pano_play&id=%@"

@interface CityBDetailViewController ()


@property (strong, nonatomic) IBOutlet UIWebView *allWebView;


@property (strong, nonatomic) IBOutlet UILabel *detailLabel;



@property (nonatomic, strong) NSString *getURL;

@property (nonatomic, strong) NSMutableArray *dataArray;

@end

@implementation CityBDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.view.backgroundColor = [UIColor whiteColor];
    //NavBar内容添加
    [self loadNavBar];
    
       [FanProgress3D showInView:self.view status:@"全景载入中...."];
    [self loadWebView];
    
    
    
}
#pragma mark - NavBar内容添加
- (void)loadNavBar{
    self.navigationItem.title=[_detailDict objectForKey:@"title"];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setBackgroundImage:[UIImage imageNamed:@"btn_nav_share"] forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:@"btn_nav_share"] forState:UIControlStateNormal];
    button.frame = CGRectMake(0, 0, 30, 30);
    [button addTarget:self action:@selector(shareClick:) forControlEvents:UIControlEventTouchUpInside];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:button];
}
#pragma mark- 分享按钮事件
- (void)shareClick:(UIButton *)sender{
    NSLog(@"%@",self.navigationItem.title);
    
    AVUser * currentUser = [AVUser currentUser];
    if (currentUser != nil ) {
        
        AVQuery *query = [AVQuery queryWithClassName:@"Collect"];
        [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
            _dataArray = [NSMutableArray array];
            
            for (int i = 0; i<objects.count; i++) {
                if ([objects[i][@"userName"] isEqualToString:[AVUser currentUser].username]) {
                        if ([objects[i][@"title"] isEqualToString:self.navigationItem.title]) {
                            
                            
                            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"成功" message:@"已经收藏!" preferredStyle:UIAlertControllerStyleAlert];
                            
                            UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                            }];
                            [alert addAction:action];
                            [self presentViewController:alert animated:YES completion:nil];
                            
                            break;
                            
                        }else{
                            
                                AVObject *userCollect = [AVObject objectWithClassName:@"Collect"];
                                [userCollect setObject:self.getURL forKey:@"webURL"];
                                [userCollect setObject:self.detailLabel.text forKey:@"detailInfo"];
                                [userCollect setObject:currentUser.username forKey:@"userName"];
                                [userCollect setObject:[_detailDict objectForKey:@"title"] forKey:@"title"];
                                [userCollect saveEventually:^(BOOL succeeded, NSError *error) {
                                    if (succeeded) {
                                        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"成功" message:@"收藏成功" preferredStyle:UIAlertControllerStyleAlert];
                                        
                                        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                                        }];
                                        [alert addAction:action];
                                        [self presentViewController:alert animated:YES completion:nil];
                                    }else{
                                        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"失败" message:@"收藏失败,请检查网络问题" preferredStyle:UIAlertControllerStyleAlert];
                                        
                                        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                                        }];
                                        [alert addAction:action];
                                        [self presentViewController:alert animated:YES completion:nil];
                                        
                                    }
                                }];
                            break;
                    
                        }
                }
            }
            
          
        }];

        

        
    }else{
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"您未登录,请您进入个人中心先进行登录" preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        }];
        [alert addAction:action];
        [self presentViewController:alert animated:YES completion:nil];
        //未登录
        NSLog(@"未登录");
    }

}



#pragma mark ---数据请求
- (void)loadWebView{
    
    
    NSURL *url=[NSURL URLWithString:[NSString stringWithFormat:kQJDetailUrl,[_detailDict objectForKeyedSubscript:@"publishPanoId"]]];
    
    self.getURL = [NSString stringWithFormat:kQJDetailUrl,[_detailDict objectForKeyedSubscript:@"publishPanoId"]];
    
    
    NSURLRequest *request=[NSURLRequest requestWithURL:url];
    
    self.allWebView.delegate=self;
    [self.allWebView loadRequest:request];

    self.detailLabel.layer.masksToBounds = YES;
    self.detailLabel.layer.cornerRadius = 10;
    self.detailLabel.font = [UIFont systemFontOfSize:15];
    self.detailLabel.textColor = [UIColor grayColor];
    
    self.detailLabel.text = [NSString stringWithFormat:@"      %@",[_detailDict objectForKey:@"description"]];
    
    
}

#pragma mark - uiwebview delegate
//开始加载
-(void)webViewDidStartLoad:(UIWebView *)webView{
    
    
}
-(void)webViewDidFinishLoad:(UIWebView *)webView{
    [FanProgress3D dismiss];
}
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
    [FanProgress3D dismissWithStatus:@"请检查网络，刷新或返回重试" afterDelay:3];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
